package com.heny.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heny.demo.common.CommonErrorCode;
import com.heny.demo.common.CommonException;
import com.heny.demo.entity.TurnPoint;
import com.heny.demo.entity.TurnPointPOJO;
import com.heny.demo.entity.TurnPointVO;
import com.heny.demo.mapper.TurnPointMapper;
import com.heny.demo.service.TurnPointService;
import com.heny.demo.strategy.FileProcessorContext;
import com.heny.demo.utils.CoordinateConverter;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
/**
 * @author litao.cc
 * @version TurnPointServiceImpl.java v0.1 t2025/4/24 1:27
 * @date 2025/4/24 1:27
 * @description TurnPointServiceImpl
 */
@Service
public class TurnPointServiceImpl extends ServiceImpl<TurnPointMapper, TurnPoint> implements TurnPointService {
    private static final Logger logger = LoggerFactory.getLogger(TurnPointServiceImpl.class);

    @Autowired
    private TurnPointMapper turnPointMapper;
    private final GeometryFactory geometryFactory = new GeometryFactory();
    @Override
    public boolean uploadTurnPoint(MultipartFile turnPointFile) {

        if (turnPointFile == null || turnPointFile.isEmpty()) {
            throw new CommonException(CommonErrorCode.FILE_EMPTY);
        }
        FileProcessorContext fileProcessorContext = new FileProcessorContext(turnPointFile);
        // 解析文件
        List<TurnPointPOJO> rawData = fileProcessorContext.execute();
        // 数据转换
        List<TurnPoint> points = null;
        try {
            points = convertToEntities(rawData);
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        } catch (TransformException e) {
            throw new RuntimeException(e);
        }
        // 批量保存
        boolean success = this.saveBatch(points);
        return true;
    }

    @Override
    public TurnPointVO getOne(Integer id) {
        return new TurnPointVO(turnPointMapper.selectById(id));
    }

    /**
     * 原始数据转换为实体对象
     */
    private List<TurnPoint> convertToEntities(List<TurnPointPOJO> rawData) throws FactoryException, TransformException {
        List<TurnPoint> points = new ArrayList<>();
        TurnPoint turnPoint;
        for (TurnPointPOJO record : rawData) {
            turnPoint = new TurnPoint();
            Point point = CoordinateConverter.convertFromCGCS2000ToWGS84(record.getX(), record.getY());
//            GeometryFactory geometryFactory = new GeometryFactory();
//            Point pointWithZ = geometryFactory.createPoint(new Coordinate(point.getX(), point.getY(), record.getZ()));
            turnPoint.setIndex(record.getIndex());
            turnPoint.setCoordinate(point);
            points.add(turnPoint);
        }

        return points;
    }

}
